﻿<%@ Control Language="C#" AutoEventWireup="true" Inherits="WorkFlow_Control_EmployeeSelect" Codebehind="EmployeeSelect.ascx.cs" %>
<table width="100%">
<tr>
    <td>
        <asp:TextBox ID="txtEmployeeNames" runat="server" Width="98%"></asp:TextBox></td>
    <td width="80px">
        <asp:Button ID="btnSelect" runat="server" Text="选择" OnClientClick="SelectEmployee();return false;" /></td>
</tr>
</table>

<asp:HiddenField ID="hiEmployees" runat="server" />
<asp:HiddenField ID="hiRoles" runat="server" />
<asp:HiddenField ID="hiPositions" runat="server" />
<asp:HiddenField ID="hiDepPositions" runat="server" />

<script language="javascript" type="text/javascript">
function OrgData(){
    var Type;
    var ID;
    var Name;
}
function GetOrgSelected(){               
    var EmployeeStr = document.getElementById("<%=hiEmployees.ClientID %>").value;
    var RoleStr = document.getElementById("<%=hiRoles.ClientID %>").value;
    var PositionStr = document.getElementById("<%=hiPositions.ClientID %>").value;
    var DepPositionStr = document.getElementById("<%=hiDepPositions.ClientID %>").value;    
    var EmployeeArr;
    if(EmployeeStr != ""){
        EmployeeArr = EmployeeStr.split(",");
    }
    var RoleArr;
    if(RoleStr!=""){
        RoleArr= RoleStr.split(",");
    }    
    var PositionArr;
    if(PositionStr != ""){
        PositionArr = PositionStr.split(",");
    }
    var DepPositionArr;
    if(DepPositionStr != ""){
        DepPositionArr = DepPositionStr.split(",");
    }    
    var iArrLength=0;
    if(EmployeeArr != null){iArrLength+=EmployeeArr.length;}
    if(RoleArr != null){iArrLength+=RoleArr.length;}
    if(PositionArr != null){iArrLength+=PositionArr.length;}
    if(DepPositionArr != null){iArrLength+=DepPositionArr.length;}
    var myOrgDatas;
    if(iArrLength != 0){
     myOrgDatas = new Array(iArrLength);
    }
    if(myOrgDatas != null){
        var arrIndex=0;
        if(EmployeeArr != null){
            for(var i=0;i<EmployeeArr.length;i++){            
                var myEmployee=new OrgData();
                myEmployee.Type="Employee";
                myEmployee.ID=EmployeeArr[i].split("|")[0];
                myEmployee.Name=EmployeeArr[i].split("|")[1];
                myOrgDatas[arrIndex]=myEmployee;
                arrIndex++;
            }
        }
        if(RoleArr != null){
            for(var i=0;i<RoleArr.length;i++){            
                var myRole=new OrgData();
                myRole.Type="Role";
                myRole.ID=RoleArr[i].split("|")[0];
                myRole.Name=RoleArr[i].split("|")[1];
                myOrgDatas[arrIndex]=myRole;
                arrIndex++;
            }
        }
        if(PositionArr != null){
            for(var i=0;i<PositionArr.length;i++){            
                var myPosition=new OrgData();
                myPosition.Type="Position";
                myPosition.ID=PositionArr[i].split("|")[0];
                myPosition.Name=PositionArr[i].split("|")[1];
                myOrgDatas[arrIndex]=myPosition;
                arrIndex++;
            }
        }
        if(DepPositionArr != null){
            for(var i=0;i<DepPositionArr.length;i++)
            {            
                var myDepPosition=new OrgData();
                myDepPosition.Type="DepPosition";
                myDepPosition.ID=DepPositionArr[i].split("|")[0];
                myDepPosition.Name=DepPositionArr[i].split("|")[1];
                myOrgDatas[arrIndex]=myDepPosition;
                arrIndex++;
            }
        }
    }
    return myOrgDatas;
}
function SelectEmployee()
{    
    var myOrgDatas = GetOrgSelected();
    var Url =  "<%= WorkflowApply.Home %>WorkFlow/OrgMapping/SelectOrg.aspx";//EmployeeSelect.aspx";
    var retValue = window.showModalDialog(Url,myOrgDatas,'dialogHeight=500px;dialogWidth=600px;help=no;scrollbars=no;status=no');        
    if(retValue != null)
    {        
        var displayText="";
        var EmployeeStr="";
        var RoleStr="";
        var PositionStr="";
        var DepPositionStr="";        
        for(var i=0;i<retValue.length;i++){   
            if(displayText == ""){displayText = retValue[i].Name;}else{displayText += "," + retValue[i].Name;}         
            if(retValue[i].Type=="Employee"){
                if(retValue[i].ID!=null &&(retValue[i].ID+"")!=""){
                    if(EmployeeStr==""){
                        EmployeeStr = retValue[i].ID+"|"+retValue[i].Name;
                    }        
                    else{
                        EmployeeStr +=","+ retValue[i].ID+"|"+retValue[i].Name;
                    }                    
                }
            }
            else if(retValue[i].Type=="Role"){
                if(retValue[i].ID!=null &&(retValue[i].ID+"")!=""){
                    if(RoleStr==""){
                        RoleStr = retValue[i].ID+"|"+retValue[i].Name;
                    }        
                    else{
                        RoleStr +=","+ retValue[i].ID+"|"+retValue[i].Name;
                    }
                }
            }
            else if(retValue[i].Type=="Position"){
                if(retValue[i].ID!=null &&(retValue[i].ID+"")!=""){
                    if(PositionStr==""){
                        PositionStr = retValue[i].ID+"|"+retValue[i].Name;
                    }        
                    else{
                        PositionStr +=","+ retValue[i].ID+"|"+retValue[i].Name;
                    }
                }
            }
            else if(retValue[i].Type=="DepPosition"){
                if(retValue[i].ID!=null &&(retValue[i].ID+"")!=""){
                    if(DepPositionStr==""){
                        DepPositionStr = retValue[i].ID+"|"+retValue[i].Name;
                    }        
                    else{
                        DepPositionStr +=","+ retValue[i].ID+"|"+retValue[i].Name;
                    }
                }
            }
        }
        document.getElementById("<%=hiEmployees.ClientID %>").value=EmployeeStr;
        document.getElementById("<%=hiRoles.ClientID %>").value=RoleStr;
        document.getElementById("<%=hiPositions.ClientID %>").value=PositionStr;
        document.getElementById("<%=hiDepPositions.ClientID %>").value=DepPositionStr;
        document.getElementById("<%=txtEmployeeNames.ClientID %>").value=displayText;
        return true;
    }
    else
    {
        return false;
    }
}
 //检查obj列表框中是否已经存在选项opt   
  function   optionExists(obj,value)   
  {   
      var   length   =   obj.options.length;   
      for   (var   i=0;i<length;i++)   {   
        if   ( obj.options[i].value   ==   value)   
            return   true;   
      }   
      return   false;  
  } 
</script>